package com.cloud.wms.ui.sort.group;


import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloud.core.utils.TimeUtils;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.sort.contract.GroupSortContract;
import com.cloud.wms.ui.sort.entity.BindInfoEntity;
import com.cloud.wms.ui.sort.presenter.GroupSortPresenter;
import com.cloud.wms.ui.sort.sku.SkuBindActivity;
import com.cloud.wms.ui.sort.sku.SkuSortActivity;
import com.cloud.wms.widget.dialog.AlertDialogBuilder;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * @Author:Chen
 * @Date: 2021-03-01 16:51
 * @Description:
 */
public class GroupBindActivity extends BaseBarActivity<GroupSortPresenter> implements GroupSortContract.View {

    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.tv_box_code)
    TextView tvBoxCode;
    @BindView(R.id.ll_box_info)
    LinearLayout llBoxInfo;
    private BindInfoEntity bindInfoEntity;
    private boolean isAdd;//是否是追加容器
    private TaskEntity taskEntity;

    @Override
    protected String barTitle() {
        return "绑定容器";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_sort_group_bind;
    }

    @Override
    protected void initEvent() {
        initScanText(scanText);
        isAdd = getIntent().getBooleanExtra(IntentKey.TYPE_KEY, false);
        taskEntity = getIntent().getParcelableExtra(IntentKey.OBJECT_KEY);
        if (taskEntity != null)
            taskEntity.setBeginTime(TimeUtils.getCurrentDateAndTime());
        if (!isAdd)
            mPresenter.checkBindContainer(taskEntity.getMainBillID());
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.checkBindContainer(taskEntity.getMainBillID());
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        if (llBoxInfo.getVisibility() == View.VISIBLE) {
            if (result.equalsIgnoreCase(tvBoxCode.getText().toString())) {
                GroupSortActivity.start(this, bindInfoEntity, taskEntity);
                this.finish();
            } else
                info("当前容器号扫描错误，请检查！");
        } else {
            mPresenter.bindContainer(taskEntity.getMainBillID(), result);
        }
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case GroupSortPresenter.CHECK_BIND_CONTAINER:
                ArrayList<BindInfoEntity> infoEntityList = (ArrayList<BindInfoEntity>) message.obj;
                if (infoEntityList.size() == 0)
                    return;
                bindInfoEntity = infoEntityList.get(infoEntityList.size() - 1);
                if (bindInfoEntity.getContainerID() > 0 || !TextUtils.isEmpty(bindInfoEntity.getContainerCode())) {
                    info("当前任务已绑定容器，请扫描确认！");
                    tvBoxCode.setText(bindInfoEntity.getContainerCode());
                    llBoxInfo.setVisibility(View.VISIBLE);
                } else if (bindInfoEntity.isPrintOrder()) {
                    new AlertDialogBuilder(this)
                            .setTitle("提示")
                            .setMessage("检测当前面单已打印，是否跳过容器操作？")
                            .setPositiveButton("跳过", (dialog, which) -> {
                                GroupSortActivity.start(this, bindInfoEntity, taskEntity);
                                finish();
                            })
                            .setNegativeButton("取消", null)
                            .show();
                } else {
                    info("请绑定容器！");
                }
                break;
            case GroupSortPresenter.BIND_CONTAINER:
                bindInfoEntity = ((BindInfoEntity) message.obj);
                GroupSortActivity.start(this, bindInfoEntity, taskEntity);
                this.finish();
                break;
        }
    }

    public static void start(Context context, boolean isAdd, TaskEntity taskEntity) {
        Intent intent = new Intent(context, GroupBindActivity.class);
        intent.putExtra(IntentKey.TYPE_KEY, isAdd);
        intent.putExtra(IntentKey.OBJECT_KEY, taskEntity);
        context.startActivity(intent);
    }
}
